#ifndef DTU_DEVICE_H
#define DTU_DEVICE_H

#include "../driver/driver_usart.h"
#include "../include/tool/log.h"
#include <iostream>
#include <string>
#include <thread>
#include <vector>


#define BASE_STATION_PROTOCOL_HEADER 0x64 // 基站协议的数据包头



#define PARSING_NULL 0 // 还没有接收到RTCM数据
#define PARSING_RTCM 1 // 解析完成RTCM数据
#define PARSING_ERROR 2 // RTCM数据错误

typedef struct
{
    uint32_t rtcmLen; 
    uint8_t rtcmData[512]; // 储存rtcm数据内容
    /* data */
}RTCM_Date;

typedef struct 
{
    uint8_t rtcmNumber;
    uint8_t rtcmLen;
    uint8_t rtcmData[200];
}RTCM_RECV;

/*私有协议数据 User protocol data*/
typedef struct
{
    uint32_t bufLen; 
    uint8_t userDataBuf[32]; // 储存rtcm数据内容
    /* data */
}USER_PROTOCOL_DATA;




class C_dtuDevice
{
private:
    /* data */
    SerialDriver *m_serial;
    RTCM_RECV m_rtcmRecvBuf[3]; // 接收过程中的数据 
    
    /* function */
public:
    /* data */
    uint8_t *m_recvBuf; // 储存串口接收到的数据
    std::vector<uint8_t> m_usratBuf;
    RTCM_Date m_RtcmData; // 接收到的RTCM数据帧
    USER_PROTOCOL_DATA m_userProtocolData; // 接收到的私有协议数据
    

    /* function */
    C_dtuDevice(const char *_path, int _speed);
    ~C_dtuDevice();

    uint16_t getCRC16(uint8_t *ptr, uint16_t length);
    uint16_t getCRC16(std::vector<uint8_t> &usratBuf,  uint16_t length);
    int32_t getUsartData(std::vector<uint8_t> &usratBuf); // 读取串口数据接口
    //int32_t getUsartData(uint8_t *p_recvBuf, uint32_t recvLen);
    void getRTCMbuf(RTCM_RECV *p_rtcmRecvBuf, uint32_t bufLen);
    int32_t parsingSerialData(std::vector<uint8_t> &usratBuf); // 解析串口数据
    
    void getUserProtocolData(uint8_t *p_recvBuf, uint32_t recvLen);
};


#endif
